package com.yjt.fresh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.activity.AtyInfoDetail;
import com.yjt.fresh.adpater.AdpRvFrgInformationList;
import com.yjt.fresh.api.ApiGetInformationList;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.bean.BeanFrgInformationList;
import com.yjt.fresh.constant.MyConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FrgInformationList extends BaseSimpleFragment {

    @BindView(R.id.rvAppFrgInformationList)
    RecyclerView mRv;
    @BindView(R.id.srlAppFrgInformationList)
    SwipeRefreshLayout mSrl;
    private AdpRvFrgInformationList mAdpList;
    private String mInfoType;

    @Override
    protected int getContentView() {
        return R.layout.app_frg_information_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        ButterKnife.bind(this, view);

    }

    @Override
    protected void initEvents() {
        initRv();
        if (getArguments() != null){
            mInfoType = getArguments().getString(MyConstants.INFO_TYPE);
            getData(mInfoType);
            mSrl.setColorSchemeColors(getResources().getColor(R.color.color_43b624));
            mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    getData(mInfoType);
                }
            });
        }
        initClick();
    }

    private void initClick() {
        mAdpList.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, AtyInfoDetail.class);
                intent.putExtra(MyConstants.INFO_DETAIL_H5, mAdpList.getItem(position).getDETAILURL());
                intent.putExtra(MyConstants.TITLE, mAdpList.getItem(position).getTITLE());
                intent.putExtra(MyConstants.TIME, mAdpList.getItem(position).getCREATETIME());
                intent.putExtra(MyConstants.AUTHOR, mAdpList.getItem(position).getAUTHOR());
                intent.putExtra(MyConstants.CONTENT, mAdpList.getItem(position).getCONTENT());
                startActivity(intent);
            }
        });
    }

    private void getData(String infoType) {
        ApiGetInformationList apiGetInformationList = new ApiGetInformationList(new HttpOnNextListener<List<BeanFrgInformationList>>() {
            @Override
            public void onNext(List<BeanFrgInformationList> beanFrgInformationLists) {
                mSrl.setRefreshing(false);
                mAdpList.setNewData(beanFrgInformationLists);
            }

            @Override
            public void onReLogin() {

            }
        },mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("ARTICLETYPE", infoType);
        apiGetInformationList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetInformationList);
    }

    private void initRv() {
        mRv.setLayoutManager(new LinearLayoutManager(mContext));
        List<BeanFrgInformationList> list = new ArrayList<>();
        mAdpList = new AdpRvFrgInformationList(list);
        mRv.setAdapter(mAdpList);
        mAdpList.bindToRecyclerView(mRv);
        mAdpList.setEmptyView(R.layout.view_empty);
    }


}
